import 'package:dio/dio.dart';
import 'dart:async';
//import 'dart:io';
import 'sever.dart';
import 'dart:convert';
import 'dart:convert' as convert;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:provider/provider.dart';
import '../provied/cartmodel.dart';
Future getHomePageContent(url) async {
  try {
    Response response;
    Dio dio = new Dio();
    var c = {"sidel": "0"};
    dio.options.contentType = "application/x-www-form-urlencoded";
    // Options options = Options(headers: {HttpHeaders.acceptHeader:"accept: application/x-www-form-urlencoded"});
    // Options options.contentType= ContentType.parse('application/x-www-form-urlencoded');
    response = await Dio().post(
      url,
      data: c,
    );
    // print(response.data.toString());
    return response.data;
  } catch (e) {
    print(e);
  }
}

Future getHttp() async {
  try {
    Response response;
    Dio dio = new Dio();
    var c = {"sidel": "0"};
    dio.options.contentType = "application/x-www-form-urlencoded";
    response = await Dio().post("http://dbyxs.xyz:3005/tion/get");
    return response;
    // print('11111${response}有');
  } catch (e) {
    print(e);
  }
}

//根据type查找商品
Future getlists(type) async {
  print(type);
  try {
    Response response;
    Dio dio = new Dio();
    var c = {"type": type};
    dio.options.contentType = "application/x-www-form-urlencoded";
    response = await Dio().post("http://dbyxs.xyz:3005/goods/list", data: c);
    return response;
    // print('11111${response}有');
  } catch (e) {
    print(e);
  }
}

//查找全部商品列表
Future getgoodslist() async {
  // print(type);
  try {
    Response response;
    Dio dio = new Dio();
    // var c={"type":type};
    dio.options.contentType = "application/x-www-form-urlencoded";
    response = await Dio().post(
      "http://dbyxs.xyz:3005/goods/detas",
    );
    return response;
    // print('11111${response}有');
  } catch (e) {
    print(e);
  }
}

//查询单个商品信息
Future goodsdetail(id) async {
  print(id);
  try {
    Response response;
    Dio dio = new Dio();
    var c = {"id": id};
    dio.options.contentType = "application/x-www-form-urlencoded";
    response = await Dio().post("http://dbyxs.xyz:3005/goods/detail", data: c);
    return response;
    // print('11111${response}有');
  } catch (e) {
    print(e);
  }
}

//登录查看是否已经注册
Future getlogin(v) async {
  try {
    Response response;
    Dio dio = new Dio();
    var c = {"user": v};
    response = await Dio().post('http://dbyxs.xyz:3005/getuser', data: c);
    print(response);
    return response;
  } catch (e) {}
}

//https://gitee.com/db121/vuepress1.git
//注册及登录
Future getAddUsers(v) async {
  try {
    Response response;
    Dio dio = new Dio();
    var data = {
      "user": json.encode(v['user']),
      "password": json.encode(v["password"])
    };
    print('小仙女${data}');
    response = await Dio().post('http://dbyxs.xyz:3005/user', data: data);
    print(response);
    return response;
  } catch (e) {}
}
//修改token
Future  addtoken(v)async{
  SharedPreferences prefs=await SharedPreferences.getInstance();
  prefs.setString('token', json.encode(v));
  print(prefs.getString('token'));
}
//查看用户名
String usernames;
Future getusertoken()async{
  SharedPreferences prefs=await SharedPreferences.getInstance();
  usernames=prefs.getString('token');
}
//token登录
Future gettoken() async {
  try {
    BaseOptions options = new  BaseOptions(
     // connectTimeout: 5000,
     // receiveTimeout: 3000,
     // contentType: 'application/x-www-form-urlencoded'
    );
     Response response;
     //options.headers['Accept']='application/json, text/plain, */*';
     //'Accept':'application/json, text/plain, */*',
    options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    String token;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    token = json.decode(prefs.getString('token'));
    Dio dio = new Dio(options);
    var data1 = {"token":token};
    print(data1);
    response =
        await Dio().post('http://dbyxs.xyz:3005/user/token', data: data1);
         var us=response.data['message']['username'];
        prefs.setString('username', us);
        print(response);
    return response;
  } catch (e) {
    print(e);
  }
}
//修改用户信息
Future getadmindetail(v)async{
   SharedPreferences prefs = await SharedPreferences.getInstance();
  try{
    Response response;
    Dio dio=new Dio();
    print(v);
     response =await Dio().post('http://dbyxs.xyz:3005/user/admin/update', data:v);
        print(response);
       
    return response;
  }catch(e){}
}
//根据用户查询信息
Future getadmin(v)async{
   SharedPreferences prefs = await SharedPreferences.getInstance();
   var c=prefs.getString('username');

  try {
  //  BaseOptions options = new  BaseOptions();
     Response response;
   // options.headers["contentType"] = "application/x-www-form-urlencoded";
   // options.contentType = "application/x-www-form-urlencoded";
    Dio dio = new Dio();
    //options
    var data1={
      "user":json.encode(c)
    };
     print('查询${data1}');
    response =
        await Dio().post('http://dbyxs.xyz:3005/user/admin', data: data1);
        print(response);
    return response;
  } catch (e) {
    print(e);
  }
}
//用户登录
Future getuserlogin(v) async {
  try {
    BaseOptions options = BaseOptions();
    Response response;
    options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    Dio dio = new Dio(options);
    var data = {"user": v['user'], "password": v["password"]};
    print(data);
    response =
        await Dio().post('http://dbyxs.xyz:3005/users/login', data: data);
    print(response);
    return response;
  } catch (e) {}
}
//商品轮播图
Future getTion()async{
  try{
    BaseOptions options = BaseOptions();
    Response response;
     options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    Dio dio=new Dio(options);
    var data={
      "sidel":'0'
    };
    //print(c);
    response=await Dio().post('http://dbyxs.xyz:3005/tion/get',data:data);
    return response;
  }catch(e){}
}
//查询轮播图商品
Future getLungoods()async{
  try{
    Response response;
    Dio dio=new Dio();
    var data={
      "type":0
    };
    response=await Dio().post('http://dbyxs.xyz:3005/goods/modiy/get',data:data);
    return response;
  }catch(e){}
}
//订单提交请求
Future  getaddorder(data)async{
  try{
    BaseOptions options = BaseOptions();
    Response response;
     options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    Dio dio=new Dio(options);
    //var d=data;
   // var c=(json.decode(data.toString())as List).cast();
    print('我要发起请求了${json.encode(data)}');
   response =await Dio().post('http://dbyxs.xyz:3005/order/add',data:data);
    //print(res)
    return response;
  }catch(e){}
}
//修改订单状态
Future getDelivers(v)async{
   try{
    BaseOptions options = BaseOptions();
    Response response;
     options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    //options
    Dio dio=new Dio(options);
    var data={
      "id":v['id'],
      "type":v['type']
    };
    // var data={
    //   "id":14,
    //   "type":2
    // };
    print('我修改订单${data}');
   response =await Dio().post('http://dbyxs.xyz:3005/order/detail',data:data);
   print(response);
    return response;
  }catch(e){}
}
//查询订单
Future getorders(v)async{
    try{
    BaseOptions options = BaseOptions();
    Response response;
     options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    Dio dio=new Dio(options);
   response =await Dio().post('http://dbyxs.xyz:3005/order/get',data:v);
    
    return response;
  }catch(e){}
}
//查询备用商品
Future getTwoGoods(v)async{
  try{
    BaseOptions options = BaseOptions();
    Response response;
     options.headers["contentType"] = "application/x-www-form-urlencoded";
    options.contentType = "application/x-www-form-urlencoded";
    Dio dio=new Dio(options);
    var data={
      "type":v
    };
   response =await Dio().post('http://dbyxs.xyz:3005/goods/modiy/get',data:data);
    
    return response;
  }catch(e){}
}
//class HomeRequest{
//   void getapi(url)async{
//     FormData formData = new FormData.fromMap({
//     "sidel": "0",

//   });

//      // Options options = Options(headers: {HttpHeaders.acceptHeader:"accept: application/json"});
//  // const parse='?sidel=0';
//  print(formData);
//   try{
//     Response response;
//     Dio dio=new Dio();
//     var c={"sidel":"0"};
//       dio.options.contentType="application/x-www-form-urlencoded";
//      // Options options = Options(headers: {HttpHeaders.acceptHeader:"accept: application/x-www-form-urlencoded"});
//    // Options options.contentType= ContentType.parse('application/x-www-form-urlencoded');
//     response=await Dio().post(url,data:c,);
//     print(response);
//     return response.data;
//   }catch(e){
//     print(e);
//   }
// }
//}
